package com.zw.pm.module.system.index;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.module.integrate.interactive.MessageModel;
import com.zw.pm.module.system.user.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pm.constant.Constant.MESSAGE_TYPE_CHANGE;
import static com.zw.pm.constant.Constant.MESSAGE_TYPE_INTERACTIVE;
import static com.zw.pm.constant.Constant.MESSAGE_TYPE_SEND;

/**
 * @Description: 主页 控制器
 *
 * @Author: China.sgl
 * @Date: 2019/9/19 09:41
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 修改密码
     *
     * @param password 新密码
     * @return json
     */
    @ResponseBody
    @PostMapping("/update/user/pwd")
    @RequiresAuthentication
    public JsonResult updatePassword(String password) {
        return jsonResult(userService.updateUserPassword(password));
    }

    /**
     * 主页
     *
     * @return 页面路径
     */
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    /**
     * 获取用户未读互动消息
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("/receiver")
    @RequiresAuthentication
    public List<MessageModel> receiver() {
        return userService.findMessageByType(MESSAGE_TYPE_INTERACTIVE);
    }

    /**
     * 获取用户未读任务下达消息
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("/send")
    @RequiresAuthentication
    public List<MessageModel> send() {
        return userService.findMessageByType(MESSAGE_TYPE_SEND);
    }

    /**
     * 获取用户未读项目变更消息
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("/change")
    @RequiresAuthentication
    public List<MessageModel> change() {
        return userService.findMessageByType(MESSAGE_TYPE_CHANGE);
    }


    /**
     * 标记所有互动消息为已读
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("/mark/interactive")
    @RequiresAuthentication
    public JsonResult markInteractive() {
        userService.markInteractive();
        return success();
    }

    /**
     * 标记所有任务下达消息为已读
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("/mark/send")
    @RequiresAuthentication
    public JsonResult markSend() {
        userService.markSend();
        return success();
    }

    /**
     * 标记所有项目变更消息为已读
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("/mark/change")
    @RequiresAuthentication
    public JsonResult markChange() {
        userService.markChange();
        return success();
    }

}
